package com.nash.demands;

import net.londatiga.android.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class RoutesActivity extends Activity {
  private ListView list;
  private DemandsActivity app;
  
  private int mYear;
  private int mMonth;
  private int mDay;
  private Cursor data=null;
  public int i;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.routes);

    app = (DemandsActivity) getParent();
    app.routesActivity=this;
    list = (ListView) findViewById(R.id.listView1);
    setAdapterRoutes();

    // Делаем контекстное меню
    //list.setFocusableInTouchMode(true);
    /*list.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
      public void onFocusChange(View arg0, boolean arg1) {
        // Log.i("SampleApp", "onFocusChanged() - view=" + arg0);
      }
    });*/
    
    
    
    
    
    
    /*list.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
      public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
        // AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle(R.string.actions);
        menu.add(0, 0, 0, R.string.createOrder);
      }
    });*/

    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> adapterView, View view, int pos, long id) {
        //Intent intent = new Intent(list.getContext(), DocTabActivity.class);
        Intent intent = new Intent(RoutesActivity.this, DocHeadActivity.class);
        Bundle b = new Bundle();
        b.putLong("demand_id", 0);
        b.putString("agent_name", ((Cursor) (list.getItemAtPosition(pos))).getString(1));
        b.putString("date", ((Cursor) (list.getItemAtPosition(pos))).getString(3));
        b.putLong("agent_id", ((Cursor) (list.getItemAtPosition(pos))).getLong(4));
        intent.putExtras(b);

        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(30);

        startActivityForResult(intent, DemandsActivity.CREATE_REQUEST_CODE);
        
        return false;
      }
    });

    //Add action item
    ActionItem addAction = new ActionItem();
    addAction.setTitle("Add");
    addAction.setIcon(getResources().getDrawable(R.drawable.ic_tab_agents_selected));

    //Accept action item
    ActionItem accAction = new ActionItem();
    accAction.setTitle("Accept");
    accAction.setIcon(getResources().getDrawable(R.drawable.ic_tab_routes_selected));

    //Upload action item
    ActionItem upAction = new ActionItem();
    upAction.setTitle("Upload");
    upAction.setIcon(getResources().getDrawable(R.drawable.ic_tab_agents_selected));


    final QuickAction mQuickAction  = new QuickAction(this);

    mQuickAction.addActionItem(addAction);
    mQuickAction.addActionItem(accAction);
    mQuickAction.addActionItem(upAction);
    
    i=1;
    mQuickAction.setAnimStyle(i++);

    //setup the action item click listener
    mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
        @Override
            public void onItemClick(int pos) {
            if (pos == 0) { //Add item selected
               Toast.makeText(RoutesActivity.this, "Add item selected", Toast.LENGTH_SHORT).show();
            } else if (pos == 1) { //Accept item selected
               Toast.makeText(RoutesActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
            } else if (pos == 2) { //Upload item selected
               Toast.makeText(RoutesActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
            }
        }
    });    

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long arg3) {
        mQuickAction.show(view);
        mQuickAction.setAnimStyle(i++);
        if (i>5)
          i=1;
      }
    });
    
  }

       
  @Override
  public void onResume() {
    super.onResume();
    if (app.mDay!=mDay || app.mMonth!=mMonth || app.mYear!=mYear){
      mDay=app.mDay;
      mMonth=app.mMonth;
      mYear=app.mYear;
      if (data!=null)
        data.close();
      setAdapterRoutes();      
    }
  }

  
/*  @Override
  public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle().equals("Создать заявку")) {
      // function1(item.getItemId());
      Intent intent = new Intent(list.getContext(), DocTabActivity.class);
      Bundle b = new Bundle();
      b.putLong("demand_id", 0);
      b.putString("agent_name", currItemOnContextMenuStr);
      b.putString("date", currItemOnContextMenuDate);
      b.putLong("agent_id", currItemOnContextMenuId);
      intent.putExtras(b);
      startActivityForResult(intent, DemandsActivity.CREATE_REQUEST_CODE);

    } else {
      return false;
    }
    return true;
  }*/

  
  private final String RoutesFields[] = { Helper.TBL_AGENT_NAME, Helper.TBL_AGENT_ADDRESS, Helper.TBL_ROUTE_DATE, Helper.TBL_ROUTE_AGENT, BaseColumns._ID };
  private final int[] RoutesTo = new int[] { android.R.id.text1, android.R.id.text2 };

  public void setAdapterRoutes() {
    StringBuilder sb = new StringBuilder()
        .append(
            "select route." + BaseColumns._ID + ",agent." + Helper.TBL_AGENT_NAME + ",agent." + Helper.TBL_AGENT_ADDRESS + ",route." + Helper.TBL_ROUTE_DATE + ",route."
                + Helper.TBL_ROUTE_AGENT + "  from " + Helper.TBL_AGENT + " agent," + Helper.TBL_ROUTE + " route where agent._ID=route.ID_AGENT and route.rdate='").append(app.mYear).append("-")
        .append(app.mMonth < 9 ? "0" : "").append(app.mMonth + 1).append("-").append(app.mDay).append("'");
    data = DemandsActivity.db.rawQuery(sb.toString(), null);
    data.moveToFirst();
    startManagingCursor(data);
    list.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, data, RoutesFields, RoutesTo));
  }
  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == DemandsActivity.CREATE_REQUEST_CODE) {
       if (resultCode == RESULT_OK) {
         if (app.orderActivity!=null)
           app.orderActivity.setAdapterOrders();
         app.tabHost.setCurrentTab(2);
       }
    }
 }
  
}


